package com.ruoyi.project.qyxy.qua.domain;

import com.ruoyi.project.qyxy.person.domain.ZzPerson;

import java.util.ArrayList;
import java.util.List;

/**
 * 监理企业人员临时类
 */
public class JlPersonTemp {
    /**
     * 注册造价工程师
     */
    private List<ZzPerson> zjPersonList = new ArrayList<>();

    /**
     * 一级注册建造师
     */
    private List<ZzPerson> jzPersonList = new ArrayList<>();

    /**
     * 一级注册建筑师
     */
    private List<ZzPerson> jzhPersonList = new ArrayList<>();

    /**
     * 一级注册结构工程师
     */
    private List<ZzPerson> jgPersonList = new ArrayList<>();

    /**
     * 注册监理工程师
     */
    private List<ZzPerson> jlPersonList = new ArrayList<>();


    /**
     * 不符合规则是否处理
     */
    public static boolean isDo = JlPersonTemp.NOT_DO;

    /**
     * 处理
     */
    public static final boolean DO = true;

    /**
     * 不处理
     */
    public static final boolean NOT_DO = false;

    /**
     * 根据资质级别轮训减少人
     * @param rank 资质级别
     * @param isQualified 是否符合资质要求
     */
    public int reduce(String rank,boolean isQualified,JlQuaTemp jlp){
        switch (rank){
            case JlQuaTemp.RUNK_JIA:
                //减少甲级人数
                return reduceJY(isQualified,jlp);
            case JlQuaTemp.RUNK_YI:
                //减少乙级人数
                return reduceJY(isQualified,jlp);
            case JlQuaTemp.RUNK_BING:
                //减少丙级人数
                return reduceBing(isQualified,jlp);
            case JlQuaTemp.RUNK_ZH:
                //减少综合人数
                return reduceZh(isQualified,jlp);
        }
        return 0;
    }

    /**
     * 打印企业拥有人员
     */
    public void printOrgPersonLog(){
        System.out.println("企业拥有造价工程师:");
        System.out.print("\t");
        for (ZzPerson zzPerson : zjPersonList) {
            System.out.print(zzPerson.getPname()+"("+zzPerson.getRegType()+")");
        }
        System.out.println();
        System.out.println("企业拥有一级注册建造师:");
        System.out.print("\t");
        for (ZzPerson zzPerson : jzPersonList) {
            System.out.print(zzPerson.getPname()+"("+zzPerson.getRegType()+")");
        }
        System.out.println();
        System.out.println("企业拥有一级注册建筑师:");
        System.out.print("\t");
        for (ZzPerson zzPerson : jzhPersonList) {
            System.out.print(zzPerson.getPname()+"("+zzPerson.getRegType()+")");
        }
        System.out.println();
        System.out.println("企业拥有一级结构工程师:");
        System.out.print("\t");
        for (ZzPerson zzPerson : jgPersonList) {
            System.out.print(zzPerson.getPname()+"("+zzPerson.getRegType()+")");
        }
        System.out.println();
        System.out.println("企业拥有注册监理工程师:");
        System.out.print("\t");
        for (ZzPerson zzPerson : jlPersonList) {
            System.out.print(zzPerson.getPname()+"("+zzPerson.getRegType()+")");
        }
        System.out.println();
    }

    /**
     * 处理综合资质
     * @param isQualified
     * @param jlp
     * @return
     */
    private int reduceZh(boolean isQualified, JlQuaTemp jlp) {
        int shortOfCount = 0;
        if(isQualified){
            if(!JlPersonTemp.isDo){
                //符合标准减少相应人数
                //监理减少60人
                for (int i = 0; i < JlQuaTemp.ZH_JL_COUNT; i++) {
                    jlp.getJlPersonList().add(jlPersonList.get(i));
                }
                //造价减少5人
                for (int i = 0; i < JlQuaTemp.ZH_ZJ_COUNT; i++) {
                    jlp.getZjPersonList().add(zjPersonList.get(i));
                }
                //其他轮训减少15人
                int count = 0;
                for (int i = 0; i < JlQuaTemp.ZH_OTHER_COUNT; i++) {
                    ZhReduceObj jzObj = new ZhReduceObj(jlp, count).invoke();
                    count = jzObj.getCount();
                    if (jzObj.is()) break;
                }
            }
        }else{
            if(JlPersonTemp.isDo){
                //不符合标准 减少全部人数
                //监理减少60人
                if(jlPersonList.size() >= JlQuaTemp.ZH_JL_COUNT){
                    for (int i = 0; i < JlQuaTemp.ZH_JL_COUNT; i++) {
                        jlp.getJlPersonList().add(jlPersonList.get(0));
                    }
                }else{
                    jlp.getJlPersonList().addAll(jlPersonList);
                    shortOfCount += JlQuaTemp.ZH_JL_COUNT - jlPersonList.size();
                    jlPersonList.clear();
                }

                //造价减少5人
                if(zjPersonList.size() >= JlQuaTemp.ZH_ZJ_COUNT){
                    for (int i = 0; i < JlQuaTemp.ZH_ZJ_COUNT; i++) {
                        jlp.getZjPersonList().add(zjPersonList.get(0));
                    }
                }else{
                    jlp.getZjPersonList().addAll(zjPersonList);
                    zjPersonList.clear();
                }
                //其他轮训减少15人
                int count = 0;
                int oldCount;
                for (int i = 0; i < JlQuaTemp.ZH_OTHER_COUNT; i++) {
                    oldCount = count;
                    ZhReduceObj jzObj = new ZhReduceObj(jlp, count).invoke();
                    count = jzObj.getCount();
                    if(oldCount == count){
                        break;
                    }
                    if (jzObj.is()) break;
                }
            }
        }
        return shortOfCount;
    }

    /**
     * 处理丙级资质
     * @param isQualified
     * @return
     */
    private int reduceBing(boolean isQualified,JlQuaTemp jlp) {
        int shortOfCount = 0;
        if(isQualified){
            if(!JlPersonTemp.isDo){
                //符合标准 减少相应人数
                for (int i = 0; i < JlQuaTemp.BING_JL_COUNT; i++) {
                    jlp.getJlPersonList().add(jlPersonList.get(i));
                }
            }
        }else{
            //不符合标准 减少全部人数
            if(JlPersonTemp.isDo){
                jlp.getJlPersonList().addAll(jlPersonList);
                //记录缺少人数
                shortOfCount = JlQuaTemp.BING_JL_COUNT - jlPersonList.size();
            }
        }
        return shortOfCount;
    }

    /**
     * 减少甲乙级资质人数
     * @param isQualified 是否符合资质条件
     */
    public int reduceJY(boolean isQualified,JlQuaTemp jlp){
        int shortOfCount = 0;
        //符合资质条件 减少对应人数
        if(isQualified){
            if(!JlPersonTemp.isDo){
                int count = 0;
                boolean flag = true;
                while (flag){
                    JyReduceObj jyReduceObj = new JyReduceObj(jlp, count).invoke();
                    count = jyReduceObj.getCount();
                    if (jyReduceObj.is()) break;
                }
            }
        }else{
            if(JlPersonTemp.isDo){
                //不符合资质条件 有多少减多少
                int count = 0;
                int oldCount;
                boolean flag = true;
                while (flag){
                    oldCount = count;
                    JyReduceObj jyReduceObj = new JyReduceObj(jlp, count).invoke();
                    count = jyReduceObj.getCount();
                    //如果没有减去人 退出循环
                    if(oldCount == count){
                        break;
                    }
                }
                //记录缺少人数
                shortOfCount = jlp.JY_MIN_SUM_COUNT - count;
            }
        }
        return shortOfCount;
    }

    public int getZjCount(){
        return zjPersonList.size();
    }

    public int getJzCount(){
        return jzPersonList.size();
    }

    public int getJzhCount(){
        return jzhPersonList.size();
    }

    public int getJgCount(){
        return jgPersonList.size();
    }

    public int getJlCount(){
        return jlPersonList.size();
    }

    public JlPersonTemp(List<ZzPerson> zjPersonList, List<ZzPerson> jzPersonList, List<ZzPerson> jzhPersonList, List<ZzPerson> jgPersonList, List<ZzPerson> jlPersonList) {
        this.zjPersonList.addAll(zjPersonList);
        this.jzPersonList.addAll(jzPersonList);
        this.jzhPersonList.addAll(jzhPersonList);
        this.jgPersonList.addAll(jgPersonList);
        this.jlPersonList.addAll(jlPersonList);
    }

    public List<ZzPerson> getZjPersonList() {
        return zjPersonList;
    }

    public List<ZzPerson> getJzPersonList() {
        return jzPersonList;
    }

    public List<ZzPerson> getJzhPersonList() {
        return jzhPersonList;
    }

    public List<ZzPerson> getJgPersonList() {
        return jgPersonList;
    }

    public List<ZzPerson> getJlPersonList() {
        return jlPersonList;
    }


    /**
     * 是否包含人员
     * @param zzPerson
     * @param list
     * @return
     */
    private boolean containsPerson(ZzPerson zzPerson , List<ZzPerson> list){
        boolean flag = false;
        for (ZzPerson person : list) {
            if(zzPerson.getIdNum().equals(person.getIdNum())){
                flag = true;
            }
        }
        return flag;
    }

    //甲乙企业减少人数处理类
    private class JyReduceObj {
        private boolean myResult;
        private JlQuaTemp jlp;
        private int count;

        public JyReduceObj(JlQuaTemp jlp, int count) {
            this.jlp = jlp;
            this.count = count;
        }

        boolean is() {
            return myResult;
        }

        public int getCount() {
            return count;
        }

        public JyReduceObj invoke() {
            //造价
            if(!zjPersonList.isEmpty()){
                for (ZzPerson zlPerson : zjPersonList) {
                    if(!containsPerson(zlPerson,jlp.getZjPersonList())){
                        jlp.getZjPersonList().add(zlPerson);
                        count++;
                        if(count == JlQuaTemp.JY_MIN_SUM_COUNT){
                            myResult = true;
                            return this;
                        }
                    }
                }
            }
            //建造师
            if(!jzPersonList.isEmpty()){
                for (ZzPerson jzPerson : jzPersonList) {
                    if(!containsPerson(jzPerson,jlp.getJzPersonList())){
                        jlp.getJzPersonList().add(jzPerson);
                        count++;
                        if(count == JlQuaTemp.JY_MIN_SUM_COUNT){
                            myResult = true;
                            return this;
                        }
                    }
                }
            }
            //建筑师
            if(!jzhPersonList.isEmpty()){
                for (ZzPerson jzhPerson : jzhPersonList) {
                    if(!containsPerson(jzhPerson,jlp.getJzhPersonList())){
                        jlp.getJzhPersonList().add(jzhPerson);
                        count++;
                        if(count == JlQuaTemp.JY_MIN_SUM_COUNT){
                            myResult = true;
                            return this;
                        }
                    }
                }
            }
            //结构
            if(!jgPersonList.isEmpty()){
                for (ZzPerson jgPerson : jgPersonList) {
                    if(!containsPerson(jgPerson,jlp.getJgPersonList())){
                        jlp.getJgPersonList().add(jgPerson);
                        count++;
                        if(count == JlQuaTemp.JY_MIN_SUM_COUNT){
                            myResult = true;
                            return this;
                        }
                    }
                }
            }
            //监理
            if(!jlPersonList.isEmpty()){
                for (ZzPerson jlPerson : jlPersonList) {
                    if(!containsPerson(jlPerson,jlp.getJlPersonList())){
                        jlp.getJlPersonList().add(jlPerson);
                        count++;
                        if(count == JlQuaTemp.JY_MIN_SUM_COUNT){
                            myResult = true;
                            return this;
                        }
                    }
                }
            }
            myResult = false;
            return this;
        }
    }

    //综合资质
    private class ZhReduceObj {
        private boolean myResult;
        private JlQuaTemp jlp;
        private int count;

        public ZhReduceObj(JlQuaTemp jlp, int count) {
            this.jlp = jlp;
            this.count = count;
        }

        boolean is() {
            return myResult;
        }

        public int getCount() {
            return count;
        }

        public ZhReduceObj invoke() {
            //建造
            if(!jzPersonList.isEmpty()){
                for (ZzPerson zjPerson : jzPersonList) {
                    if(!containsPerson(zjPerson,jlp.getJzPersonList())){
                        jlp.getJzPersonList().add(zjPerson);
                        count++;
                        if(count == JlQuaTemp.ZH_OTHER_COUNT){
                            myResult = true;
                            return this;
                        }
                    }
                }
            }
            //建筑
            if(!jzhPersonList.isEmpty()){
                for (ZzPerson jzhPerson : jzhPersonList) {
                    if(!containsPerson(jzhPerson,jlp.getJzhPersonList())){
                        jlp.getJzhPersonList().add(jzhPerson);
                        count++;
                        if(count == JlQuaTemp.ZH_OTHER_COUNT){
                            myResult = true;
                            return this;
                        }
                    }
                }
            }
            //结构
            if(!jgPersonList.isEmpty()){
                for (ZzPerson jgPerson : jgPersonList) {
                    if(!containsPerson(jgPerson,jlp.getJgPersonList())){
                        jlp.getJgPersonList().add(jgPerson);
                        count++;
                        if(count == JlQuaTemp.ZH_OTHER_COUNT){
                            myResult = true;
                            return this;
                        }
                    }
                }
            }
            myResult = false;
            return this;
        }
    }
}
